# coding=utf-8
import pytest
import operator

from Page.log import log

assert_log = log()


def log_print(fun):
    """
    校验后日志打印
    :param Expected:
    :param Actual:
    :param bool:
    :return:
    """

    def wrapped(*args):
        # 判断是否由string_assert类里面的方法使用，以便确定参数位置，类使用此装饰器时，第一个参数是类本身
        if isinstance(args[0], string_assert):
            Expected = args[1]
            Actual = args[2]
        else:
            Expected = args[0]
            Actual = args[1]
        # 获取到断言操作和断言结果，比如('大于',True)，然后进行日志输出
        result = fun(*args)
        if result[1] is False:
            assert_log.info(f"校验失败，校验预期值【{Expected}】 {result[0]} 实际值：【{Actual}】失败！")
        else:
            assert_log.info(f"校验成功，校验预期值【{Expected}】 {result[0]} 实际值：【{Actual}】成功！")

    return wrapped


@log_print
def list_assert_equal(Expected_list1, actual_list2, order: bool = True):
    """
    列表比较：https://www.cnblogs.com/lsdb/p/10275657.html
    断言列表是否相等，order是否排序后对比，默认排序后对比
    :param list1:
    :param list2:
    :param order:
    :return:
    """
    if order == True:
        return '等于', pytest.assume(sorted(Expected_list1) == sorted(actual_list2))
    else:
        return '等于', pytest.assume(Expected_list1 == actual_list2)


@log_print
def list_assert_contains(list1, list2):
    """
    list1内的元素存在于list2中
    :param list1:
    :param list2:
    :return:
    """
    return '元素存在于', pytest.assume(set(list1).issubset(set(list2)))
    # pytest.assume(set(list2).issuperset(set(list1)))


@log_print
def dict_assert(dict1, dict2):
    """
    operator.eq对比字典，自动排序后对比
    函数	                含义
    operator.lt(a, b)	a < b
    operator.le(a, b)	a <= b
    operator.eq(a, b)	a == b
    operator.ne(a, b)	a != b
    operator.gt(a, b)	a > b
    operator.ge(a, b)	a >= b
    :param dict1:
    :param dict2:
    :return:
    """
    return '等于', pytest.assume(operator.eq(dict1, dict2))


class string_assert:

    @log_print
    def eq(self, string1, string2):
        """
        string1等于string2
        :param string1:
        :param string2:
        :return:
        """
        return '等于', pytest.assume(str(string1) == str(string2))

    @log_print
    def lt(self, string1, string2):
        """
        string1小于string2
        :param string1:
        :param string2:
        :return:
        """
        return '小于', pytest.assume(str(string1) < str(string2))

    @log_print
    def le(self, string1, string2):
        """
        string1小于等于string2
        :param string1:
        :param string2:
        :return:
        """
        return '小于等于', pytest.assume(str(string1) <= str(string2))

    @log_print
    def ne(self, string1, string2):
        """
        string1不等于string2
        :param string1:
        :param string2:
        :return:
        """
        return '不等于', pytest.assume(str(string1) != str(string2))

    @log_print
    def gt(self, string1, string2):
        """
        string1大于string2
        :param string1:
        :param string2:
        :return:
        """
        return '大于', pytest.assume(str(string1) > str(string2))

    @log_print
    def ge(self, string1, string2):
        """
        string1大于等于string2
        :param string1:
        :param string2:
        :return:
        """
        return '大于等于', pytest.assume(str(string1) >= str(string2))
